<template>
    <highlightjs autodetect :code="code" />
</template>

<script>
import "highlight.js/styles/atom-one-light.css";
import 'highlight.js/lib/common'
export default {
    name: "preview-txt",
    props: ['fileId'],
    data() {
        return {
            code: ""
        }
    },
    mounted() {
        this.getFileblob()
    },
    methods: {
        async getFileblob() {
            let res = await this.$store.dispatch("getFileblob", this.fileId)
            const reader = new FileReader();

            reader.onload = () => {
                let txt = reader.result; // FileReader读取到的结果在result属性中
                this.code = txt;  // 获取的数据data
            };

            // 使用FileReader对象，将blob数据，读取为文本字符串（可以指定编码）
            // reader.readAsText(blobResult.value, encode.value); 
            reader.readAsText(res, "utf8"); 
            // console.log(res.value)
            // this.code = res
        }
    },
    watch:{
        fileId(newVal, oldVal){
            this.getFileblob()
        }
    }
}
</script>

<style scoped></style>